<?php
namespace Admin\Model;

use Think\Model\RelationModel;
use Think\Log;

class MenuModel extends RelationModel
{

    public function nodes($condition = array(), $level = 0)
    {
        $condition['level'] = array(
            'elt',
            $level
        );
        $list = $this->where($condition)
            ->field('id,menu_name as title,icon, module, action, sort_order, level')
            ->order('sort_order asc')
            ->select();
        foreach ($list as $key => $node) {
            if ($node['level'] >= 2) {
                $node['icon'] = '';
            }
            $node['url'] = __MODULE__ . "/{$node['module']}/{$node['action']}";
            $children = $this->nodes(array(
                'pid' => $node['id']
            ), $level);
            $node['spread'] = false;
            if (! empty($children)) {
                $node['children'] = $children;
            }
            $list[$key] = $node;
        }
        return $list;
    }

    public function menus($condition = array(), $level = 0)
    {
        $condition['level'] = array(
            'elt',
            $level
        );
        $list = $this->where($condition)
            ->field('id,menu_name as name, sort_order, level')
            ->order('sort_order asc')
            ->select();
        foreach ($list as $key => $node) {
            $children = $this->menus(array(
                'pid' => $node['id']
            ), $level);
            $node['spread'] = false;
            if (! empty($children)) {
                $node['children'] = $children;
            }
            $list[$key] = $node;
        }
        return $list;
    }

    public function menusTree($condition = array())
    {
        $field = 'id as value,menu_name as title, is_default';
        $list = $this->where($condition)
            ->field($field)
            ->order('sort_order asc')
            ->select();
        foreach ($list as $key => $value) {
            $children = $this->menusTree(array(
                'pid' => $value['value']
            ));
            $value['data'] = array();
            if (! empty($children)) {
                $value['data'] = $children;
            }
            $list[$key] = $value;
        }
        return $list;
    }

    public function menus_privilege($privileges)
    {
        $field = 'id as value,menu_name as title, is_default, pid, level';
        $menu_ids = array();
        foreach ($privileges as $privilege) {
            $menu_ids[] = $privilege['menu_id'];
        }
        $list = $this->where(array(
            'id' => array(
                'in',
                $menu_ids
            )
        ))
            ->field($field)
            ->order('sort_order asc')
            ->select();
        $menus = array();
        $menus = $this->menu_array_to_tree($list);
        return $menus;
    }

    public function menuList($user_id)
    {
        $menuList = array();
        $table = array(
            C('DB_PREFIX') . C('RBAC_ROLE_TABLE') => 'role',
            C('DB_PREFIX') . C('RBAC_ROLE_USER_TABLE') => 'role_user',
            C('DB_PREFIX') . C('RBAC_ROLE_PRIVILEGE_TABLE') => 'role_privilege',
            C('DB_PREFIX') . C('RBAC_PRIVILEGE_TABLE') => 'privilege',
            C('DB_PREFIX') . C('RBAC_PRIVILEGE_MENU_TABLE') => 'privilege_menu',
            C('DB_PREFIX') . C('RBAC_MENU_TABLE') => 'menu'
        );
        $where = "role_user.user_id='{$user_id}' and role_user.role_id = role.id and role_privilege.role_id = role.id and role.status = 0 and role_privilege.privilege_id = privilege.id and privilege.id = privilege_menu.privilege_id and privilege_menu.menu_id = menu.id and menu.status = 0 and menu.pid = 0";
        Log::record($where);
        $menuList = $this->table($table)
            ->where($where)
            ->field('menu_id as id,menu_name as title,icon')
            ->order('sort_order asc')
            ->select();
        Log::record(serialize($menuList));
        return $menuList;
    }

    private function menu_array_to_tree($menus, $find_pid = 0, $l = 3, $pid = 'pid', $id = 'value', $child = 'data', $level = 'level')
    {
        $data = array();
        foreach ($menus as $k => $menu) {
            if ($menu[$pid] == $find_pid) {
                $menu[$child] = array();
                $menu['checked'] = true;
                $menu['disabled'] = true;
                unset($menus[$k]);
                $data[] = $menu;
            }
        }
        if (! empty($data)) {
            foreach ($data as $key => $value) {
                $childs = $this->menu_array_to_tree($menus, $value[$id], $l, $pid, $id, $child);
                $value[$child] = array();
                if (! empty($childs)) {
                    $value[$child] = $childs;
                }
                $data[$key] = $value;
            }
        }
        return $data;
    }
}

